import mongoose from 'mongoose';
import dotenv from 'dotenv';
import users from './data/users.js';
import products from './data/products.js';
import User from './models/userModel.js';
import Product from './models/productModel.js';
import Order from './models/orderModel.js';
import connectDB from './config/db.js';

dotenv.config();
connectDB();

//插入样本数据到数据库
const importData = async ()=>{
    try {
        //清空数据库中的样本数据
        await Order.deleteMany();
        await User.deleteMany();
        await Product.deleteMany();

        //实现数据的插入
        const createdUsers = await User.insertMany(users);
        const adminUser = createdUsers[0]._id;
        const sampleProducts = products.map(product=>{
            return {...product,user:adminUser}
        });

        await Product.insertMany(sampleProducts);
        //成功后进行的处理
        console.log('样本数据插入成功');
        process.exit();
    } catch (error) {
        console.error(error);
        process.exit(1);
    }
}

//销毁样本数据
const destroyData = async ()=>{
    try {
        //清空数据库中的样本数据
        await Order.deleteMany();
        await User.deleteMany();
        await Product.deleteMany();

        //成功后进行的处理
        console.log('样本数据销毁成功');
        process.exit();
    } catch (error) {
        console.error(error);
        process.exit(1);
    }
}

//判断命令行执行的函数
if(process.argv[2] === '-d'){
    destroyData();
}else{
    importData();
}